package day3.maketVIP;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Market {
    private String address;
    private String name;
    Map<String,People> map=new HashMap<>();
    {
        Calendar c = Calendar.getInstance();
        People p = new People("10001","刘乐","666666",100,"13456789077",c.getTime());
        map.put(p.getVipNumber(),p);
    }
    //开卡
    public boolean addMember(People people){
        if (!(map.containsKey(people.getVipNumber()))) {
            map.put(people.getVipNumber(), people);
            System.out.println("开卡成功");
            people.show();
            return true;
        }
        return false;
    }
    //登录
    public People login(String vipNumber,String password){
        if (map.containsKey(vipNumber)){
            if (map.get(vipNumber).getPassword().equals(password)){
                System.out.println("登录成功");
                return map.get(vipNumber);
            }
            else System.out.println("登录失败：账号密码错误！");
            return null;
        }
        else {
            System.out.println("登录失败：账号输入错误，不存在此账号！");
            return null;
        }
    }

    //显示所有会员基本信息
    public void show(){
        Set<Map.Entry<String, People>> e = map.entrySet();
        for (Map.Entry x:e){
            ((People)x.getValue()).show();
        }
    }
    //增加积分
    public void addcredit(People people,int credit){
        if (map.containsKey(people.getVipNumber())){
            map.get(people.getVipNumber()).changecredit(credit);
        }
        else System.out.println("会员信息错误：无该会员！");
    }
    //查询积分
    public void lookcredit(People people){
        if (map.containsKey(people.getVipNumber())){
            System.out.println("该会员剩余积分："+map.get(people.getVipNumber()).getCredit());
        }
        else System.out.println("会员信息错误：无该会员！");
    }

    //修改密码
    public void changepassword(People people,String password){
        if (map.containsKey(people.getVipNumber())){
            if (map.get(people.getVipNumber()).getPassword().equals(password)){
                System.out.println("修改密码失败：与原密码重复！");
            }
            else {
                map.get(people.getVipNumber()).setPassword(password);
                System.out.println("密码修改成功！");
            }
        }
        else System.out.println("会员信息错误：无该会员！");
    }
    //积分兑换
    public void convert(People people,int index){
        if (map.containsKey(people.getVipNumber())){
            switch (index){
                case 1:
                    if (map.get(people.getVipNumber()).getCredit()>=300){
                        map.get(people.getVipNumber()).changecredit(-300);
                        System.out.println("会员"+people.getName()+"成功使用300积分兑换纸巾一包！");
                        break;
                    }
                    else {
                        System.out.println("兑换失败：积分不足！");
                        break;
                    }
                case 2:
                    if (map.get(people.getVipNumber()).getCredit()>=1000){
                        map.get(people.getVipNumber()).changecredit(-1000);
                        System.out.println("会员"+people.getName()+"成功使用1000积分兑换血压计一台！");
                        break;
                    }
                    else {
                        System.out.println("兑换失败：积分不足！");
                        break;
                    }
                case 3:
                    if (map.get(people.getVipNumber()).getCredit()>=10000){
                        map.get(people.getVipNumber()).changecredit(-10000);
                        System.out.println("会员"+people.getName()+"成功使用1000积分兑换iphone一台！");
                        break;
                    }
                    else {
                        System.out.println("兑换失败：积分不足！");
                        break;
                    }
                default:
                    System.out.println("兑换失败：无该类型商品");
                    break;
            }
        }
        else System.out.println("会员信息错误：无该会员！");
    }

    public Market(String address, String name) {
        this.address = address;
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
